<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Us extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
	}

	public function index(){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
		$this->load->model('kategori','',true);
		$this->load->model('user','',true);
		$menu['kategori'] = $this->kategori->get_all();
		$data['user'] = $this->user->get_list();
		$this->load->view('header');
		$this->load->view('menu',$menu);
		$this->load->view('user',$data);
		$this->load->view('footer');
	}

	public function insert(){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
		$data = array(
                    'username' => $this->input->post('username'),
                    'password' => md5($this->input->post('password'))
                    );
		$this->load->model('user','',true);
		$this->form_validation->set_rules('username', 'username', 'trim|required|is_unique[user.username]');
		if ($this->form_validation->run() == FALSE) {
			$this->load->model('kategori','',true);
			$this->load->model('user','',true);
			$menu['kategori'] = $this->kategori->get_all();
			$data['user'] = $this->user->get_list();
			$this->load->view('header');
			$this->load->view('menu',$menu);
			$this->load->view('user',$data);
			$this->load->view('footer');
		} else {
			$this->user->insert($data);
			redirect('us');
		}
		
	}

	public function delete($data){
		if ($this->session->userdata('isLoggedIn')){
            $menu['isLoggedIn'] = true;
        }else {
        	redirect('site');
        }
		$this->load->model('user','',true);
		$this->user->delete($data);
		redirect('us');
	}
}